package cn.jsj.dao;

import java.util.List;
import java.util.ListIterator;

import org.hibernate.criterion.Restrictions;
import cn.jsj.pojo.TblLawyer;

/**
 * 律师信息处理类
 * @author LI
 *
 */

public class TblLawyerDAO {
	/**
	 * 验证登陆用户是否存在
	 * @param ui：登陆用户对象
	 * @return：用户对象：null：不存在
	 */
	public TblLawyer userValidate(TblLawyer ui){
		// 取得session对象
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List userList = session.createCriteria(TblLawyer.class).add(
				Restrictions.eq("lawyerName", ui.getLawyerName()))
				.add(Restrictions.eq("lawyerPwd", ui.getLawyerPwd()))
				.add(Restrictions.eq("deleteLabel", 0))
				.list();
		// 提交事务
		tx.commit();
		session.close();
		//如果用户存在
		if(userList.size()!=0)
		{
			TblLawyer user=(TblLawyer)userList.get(0);
		return user;
		}
		else{
			return null;
		}
		}

	/**
	 * 获取律师信息条数
	 * @return:律师信息条数
	 */
	public int getLawyerInfoNumb(){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select count(*) from TblLawyer t where deleteLabel=?";
		List result=session.createQuery(hql)
		.setInteger(0, 0)
		.list();
		// 提交事务
		tx.commit();
		session.close();
		//获得结果的迭代器
		 ListIterator iterator = result.listIterator();
		 Long rows =(Long)iterator.next();
		 return Integer.parseInt(rows.toString());
	}

	/**
	 * 获取指定页面要显示的律师信息
	 * @param startNumb：从第几条开始
	 * @param totalNumb：这一页显示几条
	 * @return：这一页要显示的律师信息队列
	 */
	public java.util.List<TblLawyer> getTheLawyerInfo(cn.jsj.bgtodb.PageRestrictProperty prp){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("deleteLabel", 0))
		.setFirstResult(prp.getStartNumb())
		.setMaxResults(prp.getTotalNumb())
		.list();
		// 提交事务
		tx.commit();
		session.close();
		return result;
	}


	/**
	 * 获得要查询的律师信息
	 * @param lawyerName：律师的名字
	 * @param positionNumber：律师的职位
	 * @param positionNumber：从第几条开始
	 * @param positionNumber：这一页显示几条
	 * @return：符合条件的律师信息队列
	 */
	public java.util.List<TblLawyer> getSearchLawyerInfo(cn.jsj.bgtodb.SearchLawyerInfoProperty slip){
		/*
		 * 有三种情况
		 * 1.lawyerName为NULL positionNumber不为0
		 * 2.lawyerName不为NULL positionNumber为0
		 * 3.lawyerName不为NULL positionNumber不为0
		 */

		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//查询结果队列
		List result=new java.util.LinkedList();
		//第一种情况
		if(slip.getLawyerName().length()==0&&slip.getPositionNumber()!=0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("positionName", slip.getPositionNumber()))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(slip.getStartNumb())
		    .setMaxResults(slip.getTotalNumb())
			.list();
		}

		//第二种情况
		if(slip.getLawyerName().length()!=0&&slip.getPositionNumber()==0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("lawyerName", slip.getLawyerName()))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(slip.getStartNumb())
		    .setMaxResults(slip.getTotalNumb())
			.list();
		}

		//第三种情况
		if(slip.getLawyerName().length()!=0&&slip.getPositionNumber()!=0){
			result=session.createCriteria(TblLawyer.class)
			.add(Restrictions.eq("lawyerName", slip.getLawyerName()))
			.add(Restrictions.eq("positionName", slip.getPositionNumber()))
			.add(Restrictions.eq("deleteLabel", 0))
			.setFirstResult(slip.getStartNumb())
		    .setMaxResults(slip.getTotalNumb())
			.list();
		}

		// 提交事务
		tx.commit();
		session.close();
		return result;
	}


	/**
	 * 获得要查询的律师信息条数
	 * @param slip：查询参数类
	 * @return：符合条件的律师信息条数
	 */
	public int getSearchLawyerInfoNumb(cn.jsj.bgtodb.SearchLawyerInfoProperty slip){
		/*
		 * 有三种情况
		 * 1.lawyerName为NULL positionNumber不为0
		 * 2.lawyerName不为NULL positionNumber为0
		 * 3.lawyerName不为NULL positionNumber不为0
		 */

		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//查询结果队列
		List result=new java.util.LinkedList();

		//第一种情况
		if(slip.getLawyerName().length()==0&&slip.getPositionNumber()!=0){
			String hql="select count(*) from TblLawyer where positionName=? and deleteLabel=?";
			result=session.createQuery(hql)
			.setInteger(0, slip.getPositionNumber())
			.setInteger(1, 0)
			.list();
		}

		//第二种情况
		if(slip.getLawyerName().length()!=0&&slip.getPositionNumber()==0){
			String hql="select count(*) from TblLawyer where lawyerName=? and deleteLabel=?";
			result=session.createQuery(hql)
			.setString(0, slip.getLawyerName())
			.setInteger(1, 0)
			.list();
		}

		//第三种情况
		if(slip.getLawyerName().length()!=0&&slip.getPositionNumber()!=0){
			String hql="select count(*) from TblLawyer where lawyerName=? and positionName=?  and deleteLabel=?";
			result=session.createQuery(hql)
			.setString(0, slip.getLawyerName())
			.setInteger(1, slip.getPositionNumber())
			.setInteger(2, 0)
			.list();
		}

		// 提交事务
		tx.commit();
		session.close();
		//获得结果的迭代器
		 ListIterator iterator = result.listIterator();
		 Long rows =(Long)iterator.next();
		 return Integer.parseInt(rows.toString());
	}


	/**
	 * 修改律师信息
	 * @param tl：修改之后的律师信息对象
	 */
	public void modifyLawyerInfo(TblLawyer tl){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();

		//获得时间字符串表示
		java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd");
		String dateString=df.format(tl.getBirthTime());
		String hql="update TblLawyer m set m.lawyerName=?,m.lawyerSex=?,m.politicalStatus=?,m.businessNumber=?,m.honestArchives=?,m.lawyerPhone=?,m.educationalLevel=?,m.identityNumber=?,m.annSurRegInfo=?,m.positionName=?,m.graduateSchool=?,m.lawyerEmail=?,m.birthTime=?" +
				",m.lawyerComeTime=?,m.lawyerLeaveTime=?,m.lawyerAchriveInfo=?,m.householdAddr=?,m.achriveAddr=?,m.eligibleNumber=?where m.lawyerId=?";
		session.createQuery(hql).setString(0,tl.getLawyerName())
		.setString(1,tl.getLawyerSex())
		.setString(2,tl.getPoliticalStatus())
		.setString(3, tl.getBusinessNumber())
		.setString(4, tl.getHonestArchives())
		.setString(5, tl.getLawyerPhone())
		.setString(6, tl.getEducationalLevel())
		.setString(7, tl.getIdentityNumber())
		.setString(8, tl.getAnnSurRegInfo())
		.setInteger(9, tl.getPositionName())
		.setString(10, tl.getGraduateSchool())
		.setString(11, tl.getLawyerEmail())
		.setString(12,dateString)
		.setDate(13, tl.getLawyerComeTime())
		.setDate(14, tl.getLawyerLeaveTime())
		.setString(15, tl.getLawyerAchriveInfo())
		.setString(16, tl.getHouseholdAddr())
		.setString(17, tl.getAchriveAddr())
		.setString(18, tl.getEligibleNumber())
		.setInteger(19, tl.getLawyerId())
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();

	}

	/**
	 * 删除律师信息
	 * @param lawyerId：要被删除的律师信息ID
	 * @return:1：删除成功
	 */
	public int deleteLawyerInfo(int lawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="update TblLawyer m set m.deleteLabel=?where m.lawyerId=?";
		session.createQuery(hql)
		.setInteger(0, 1)
		.setInteger(1, lawyerId)
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}


	/**
	 * 根据实习老师id获取实习老师对象
	 * @param lawyerId：实习老师ID
	 * @return：实习老师对象
	 */
	public TblLawyer getLawyerInfoFromId(int lawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("lawyerId", lawyerId))
		.add(Restrictions.eq("deleteLabel", 0))
		.list();
		// 提交事务
		tx.commit();
		session.close();

		//获得对应的律师对象
		TblLawyer tl=(TblLawyer)result.get(0);
		return tl;
	}


	/**
	 * 根据实习老师姓名获取实习老师id
	 * @param lawyerName：实习老师姓名
	 * @return：实习老师ID  如果没有 返回 0
	 */
	public int getLawyerId(String lawyerName){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("lawyerName", lawyerName))
		.list();
		// 提交事务
		tx.commit();
		session.close();

		if(result.size()!=0){
		//获得对应的律师对象
		TblLawyer tl=(TblLawyer)result.get(0);
		return tl.getLawyerId();
		}
		else{
			return 0;
		}
	}


	/**
	 * 修改用户密码
	 * @param oldPwd：旧密码
	 * @param newPwd：新密码
	 * @param lawyerId：用户id
	 * @return：1:修改成功 0：修改失败
	 */
	public int modifyPassword(String oldPwd,String newPwd,int lawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("lawyerId", lawyerId))
		.list();

		TblLawyer lawyer=(TblLawyer)result.get(0);
		//得到用户密码
		String password=lawyer.getLawyerPwd();
		//判断输入的旧密码是否正确
		if(password.equalsIgnoreCase(oldPwd))
		{
			//修改用户密码
			String hql="update TblLawyer m set m.lawyerPwd=?where m.lawyerId=?";
			session.createQuery(hql)
			.setString(0, newPwd)
			.setInteger(1, lawyerId)
			.executeUpdate();
			// 提交事务
			tx.commit();
			session.close();
			return 1;
		}
		else{
			// 提交事务
			tx.commit();
			session.close();
			return 0;
		}
	}

	/**
	 * 修改用户的联系方式
	 * @param contactWay：新的联系方式
	 * @param lawyerId：用户id
	 * @return：1 修改成功
	 */
	public int modifyContactWay(String contactWay,int lawyerId){
		org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//修改用户密码
		String hql="update TblLawyer m set m.lawyerPhone=?where m.lawyerId=?";
		session.createQuery(hql)
		.setString(0, contactWay)
		.setInteger(1, lawyerId)
		.executeUpdate();
		// 提交事务
		tx.commit();
		session.close();
		return 1;
	}


	/**
	 * 验证用户签名是否有效
	 * @param lawyerName：律师姓名
	 * @param passWord：律师签名，即密码
	 * @return：1：签名有效 0：签名无效
	 */
    public int signedVerify(String lawyerName,String passWord){
    	org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		//修改用户密码
		List result=session.createCriteria(TblLawyer.class)
		.add(Restrictions.eq("lawyerName", lawyerName))
		.list();
		// 提交事务
		tx.commit();
		session.close();
		//获得用户密码
		String userPwd="";
		if(result.size()!=0){
			TblLawyer tl=(TblLawyer)result.get(0);
			userPwd=tl.getLawyerPwd();
		}
		//比较密码是否相等
		if(userPwd.equalsIgnoreCase(passWord)){
			return 1;
		}
		else{
			return 0;
		}

    }

    /**
     * 新增加一个律师信息
     * @param tl：律师对象
     * @return：1：添加成功
     */
    public int saveLawyerInfo(cn.jsj.pojo.TblLawyer tl){
    	org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		session.save(tl);
		// 提交事务
		tx.commit();
		session.close();
    	return 1;
    }


    /**
     * 加载所有律师姓名
     * @return：律师姓名队列
     */
    public java.util.List<cn.jsj.object.LawyerNames> getLawyerNames(){

    	org.hibernate.Session session = HBUtils.getSession();
		// 新建事务对象
		org.hibernate.Transaction tx = session.beginTransaction();
		// 开始事务
		tx.begin();
		String hql="select new cn.jsj.object.LawyerNames(l.lawyerId,l.lawyerName) from TblLawyer l";
		List result=session.createQuery(hql).list();
		// 提交事务
		tx.commit();
		session.close();
    	return result;
    }











}